package testcase;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import java.util.*;
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.xml.sax.InputSource;



public class Builder {

	/**
	 * @param args
	 */
	private String[] region = { "AP", "EMEA", "NA", "SA" };
	private String filename;
	private boolean called;
	private String mail;
	private String roleName;
	private int[] anzahlCases = { 10, 10, 7,  7};
	public Builder(String filename){
		called = false;
		this.filename = filename;
		mail = "Mail 1";
		roleName = "User";
		
	}
	
	public void writeData(String testName, String testDescription,
			ArrayList<String> stepsList, ArrayList<String> resultsList){
		if(!called){
			SAXBuilder builder = new SAXBuilder();
			File file = new File("C:/TL.xml");
			Document doc = null;
			try {
				doc = builder.build(file);
			} catch (JDOMException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
			Element root = doc.getRootElement();
			root.removeChildren("testsuite");
			root.setAttribute("name", mail);
		
			
			Element testcase = new Element("testcase");
			testcase.setAttribute("name", testName);
			//testsuite.addContent(testcase);
			
			Element summary = new Element("summary");
			CDATA sumtext = new CDATA(testDescription);
			summary.addContent(sumtext);
			testcase.addContent(summary);
			
			Element executiontype = new Element("execution_type");
			CDATA extext = new CDATA("1");
			executiontype.addContent(extext);
			testcase.addContent(executiontype);
			
			Element steps = new Element("steps");
			testcase.addContent(steps);
			for (int i = 0; i < stepsList.size(); i++) {
				
				Element step = new Element("step");
				steps.addContent(step);
				
				Element stepnumber = new Element ("step_number");
				CDATA steptext = new CDATA("" + (i+1));
				stepnumber.addContent(steptext);
				step.addContent(stepnumber);
				
				Element actions = new Element ("actions");
				CDATA actiontext = new CDATA (stepsList.get(i));
				actions.addContent(actiontext);
				step.addContent(actions);
				
				Element expectedresults = new Element("expectedresults");
				CDATA exprestext = new CDATA (resultsList.get(i));
				expectedresults.addContent(exprestext);
				step.addContent(expectedresults);
			}
			
			
			for (int i = 0; i < region.length; i++) {
				Element testsuite = new Element("testsuite");
				testsuite.setAttribute("name", region[i]);
				for (int j = 0; j < anzahlCases[i]; j++) {
					Element role = new Element("testsuite");
					role.setAttribute("name", roleName + "-" + (j+1));
					//System.out.println(testcase.getAttributes().toString());
					Element testcase2 = (Element) testcase.clone();
					role.addContent(testcase2);
					testsuite.addContent(role);
				}
				root.addContent(testsuite);
			}
			
			XMLOutputter outp = new XMLOutputter(); 
			
			int index = filename.lastIndexOf('.');
		      if (index>0 && index <= filename.length() - 2 ) {
		    	  filename = filename.substring(0, index);
		      } 
		    
		    
			try {
				outp.output(doc, new FileOutputStream(new File(filename + ".xml")));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			called = true;
	}

	}
	
	/*
	public static void main(String[] args) throws Exception{
		
		SAXBuilder builder = new SAXBuilder();
		File file = new File("C:/TL.xml");
		Document doc = builder.build(file);
		
		
		Element root = doc.getRootElement();
		root.removeChildren("testsuite");
		
	
		
		Element testcase = new Element("testcase");
		testcase.setAttribute("name", "Initial Timelines");
		//testsuite.addContent(testcase);
		
		Element summary = new Element("summary");
		CDATA sumtext = new CDATA("Summary of Initial Timelines");
		summary.addContent(sumtext);
		testcase.addContent(summary);
		
		Element executiontype = new Element("execution_type");
		CDATA extext = new CDATA("1");
		executiontype.addContent(extext);
		testcase.addContent(executiontype);
		
		Element steps = new Element("steps");
		testcase.addContent(steps);
		
		for (int i = 0; i < 2; i++) {
			Element step = new Element("step");
			steps.addContent(step);
			
			Element stepnumber = new Element ("step_number");
			CDATA steptext = new CDATA("" + (i+1));
			stepnumber.addContent(steptext);
			step.addContent(stepnumber);
			
			Element actions = new Element ("actions");
			CDATA actiontext = new CDATA ("Rollout Provider logs in" + i);
			actions.addContent(actiontext);
			step.addContent(actions);
			
			Element expectedresults = new Element("expectedresults");
			CDATA exprestext = new CDATA ("Rollout Provider logged in" + i);
			expectedresults.addContent(exprestext);
			step.addContent(expectedresults);
		}
		
		
		for (int i = 0; i < 1; i++) {
			Element testsuite = new Element("testsuite");
			//testsuite.setAttribute("name", region[i]);
			for (int j = 0; j < 1; j++) {
				Element role = new Element("testsuite");
				role.setAttribute("name", "Rollout-" + (j+1));
				//System.out.println(testcase.getAttributes().toString());
				Element testcase2 = (Element) testcase.clone();
				role.addContent(testcase2);
				testsuite.addContent(role);
			}
			root.addContent(testsuite);
		}
		
		
		
		
		
		
		/*
		System.out.println(root.getName());
		root.setAttribute("name", "TestTest");
		System.out.println(root.getChild("details").getContent());
		
		Element details = root.getChild("details");
		details.removeContent();
		CDATA cdata = new CDATA("Test");
		details.addContent(cdata);
		
		
		
		Element test = new Element("NeuesElement");
		details.addContent(test);
		
		List children = root.getChildren();
		for (Object element : children){
			Element aktuellesElement = (Element) element;
			System.out.println(element.toString());
		}
		
		
		
		List listParentElements = root.getChildren("testsuite");
		System.out.println(listParentElements.size());
		for(int i=0; i<listParentElements.size(); i++){
			
			Element elMain = (Element)(listParentElements.get(i));
			if (null == elMain) continue;
			String s = elMain.getAttributeValue("Initial Timelines - Timeline Maintain");
			if (null == s || !s.equals("Test")) continue;
			elMain.setAttribute("Test2","Test3");
		}
			XMLOutputter outp = new XMLOutputter(); 
			outp.output(doc, new FileOutputStream(new File("C:/New.xml")));
		*/	
		
		
		
		
		
		
				
		
		/*XMLOutputter outputter = new XMLOutputter();
		FileOutputStream output = new FileOutputStream("file.xml");
		outputter.output(doc,output);*/

}
